﻿using AutoMapper;
using MediatR;
using S.Archives.Api.Application.Command;
using S.Archives.Infrstructre.InterFace;

namespace S.Archives.Api.Application.CommandHandlers
{
    public class SheepFileUpdateStateCommandHandler : IRequestHandler<SheepFileUpdateStateCommand, bool>
    {
        private readonly ISheepFileRepository sheepFileRepository;
        private readonly IMapper mapper;
        public SheepFileUpdateStateCommandHandler(ISheepFileRepository sheepFileRepository, IMapper mapper)
        {
            this.sheepFileRepository = sheepFileRepository;
            this.mapper = mapper;
        }

        /// <summary>
        /// 档案状态修改
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<bool> Handle(SheepFileUpdateStateCommand request, CancellationToken cancellationToken)
        {
            bool r = false;
            //需要修改的羊只档案
            var Sheeplist = sheepFileRepository.GetAllAsync().Result.FirstOrDefault(x=>x.EarTag == request.EarTag);
            if (Sheeplist != null)
            {
                //修改繁殖状态
                if (request.Uflag == 1)
                {
                    Sheeplist.ReproductiveState = request.UState;
                    Sheeplist.ReproductiveStateDate = request.ReproductiveStateDate;
                }
                //修改在场状态
                else if (request.Uflag == 2)
                {
                    Sheeplist.PresenceState= request.UState;
                }
                //是否死亡
                else if (request.Uflag == 3)
                {
                    Sheeplist.IsPassAway = true;
                }
                //是否淘汰
                else if (request.Uflag == 4)
                {
                    Sheeplist.IsElimination = true;
                }
                //修改时间
                Sheeplist.UpdateDate= DateTime.Now;
               r = await sheepFileRepository.UpdateAsync(Sheeplist);
            }
            return r;
           

        }
    }
}
